/* low-level functions */

/* memory address translation and protection */
void ll_setmatp (void * pgtb);
void ll_clrmatp ();

/* Biscuit-Console */
void ll_putc (int c);
int ll_getc ();

/* debug */
void ll_testnum (int n);
void ll_teststr (char * str);

/* machine control */ 
#define MIE_MASK 0x4
#define MPIE_MASK 0x80
#define MPP_MASK 0x1800
#define MIE_SHFT 0x2
#define MPIE_SHFT 0x7
#define MPP_SHFT 0xB
#define set_mstatus_mpie(mstatus,mpie) (mstatus = ((mstatus&~MPIE_MASK)|((mpie&0x1)<<MPIE_SHFT)))
#define set_mstatus_mie(mstatus,mie) (mstatus = ((mstatus&~MIE_MASK)|((mie&0x1)<<MIE_SHFT)))
#define set_mstatus_mpp(mstatus,mpp) (mstatus = ((mstatus&~MPP_MASK)|((mpp&0x3)<<MPP_SHFT)))
#define clr_mstatus_mie(mstatus) (mstatus &= ~(1<<MIE_SHFT))
#define mstatus_mpie(mstatus) ((mstatus&MPIE_MASK)>>MPIE_SHFT)
#define mstatus_mie(mstatus) ((mstatus&MIE_MASK)>>MIE_SHFT)
#define mstatus_mpp(mstatus) ((mstatus&MPP_MASK)>>MPP_SHFT)
void ll_w_mtvec (void * tvec);
void ll_w_mepc (void * addr);
void* ll_r_mepc ();
int ll_r_mstatus ();
void ll_w_mstatus (int status);
int ll_r_mstatus ();
int ll_r_mcause ();
int ll_r_mscratch ();
void ll_w_mscratch (int scratch);
int ll_r_mscratch ();
int ll_r_mtval ();

int ll_r_sp ();

/* halt */
int ll_halt ();

/* timer */
int ll_r_mtime ();
void ll_w_mtimecmp (int time);



/* Biscuit-MagicFS */
#define MFS_FETCH 1
#define MFS_RM 2
#define MFS_READ 3
#define MFS_WRITE 4

#define MFSCTL_INVOKE 0x1
#define MFSCTL_DEVREADY 0x2

/* MagicFS函数
 * 参数 path:文件路径, off:文件偏移地址, buf:目标缓冲区, sz:需读/写字节数 
 * int fetch (&lsbuf); // 将文件名填充至数组,并返回文件个数
 * int rm (char * path);
 * int read (char * path, unsigned int off, void * buf, unsigned int sz);
 * int write (char * path, unsigned int off ,void * buf, unsigned int sz);
 */
int ll_mfs_invoke (int,int,int,int,int);
int ll_w_mfscratch (int);
int ll_r_mfscratch ();
int ll_r_mfsfunct ();
int ll_r_mfsret ();
int ll_r_mfsctl ();